package com.atguigu.tingshu.account.service.impl;
import java.util.UUID;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

	private final RechargeInfoMapper rechargeInfoMapper;
	private final UserAccountService userAccountService;
	@Autowired
	public RechargeInfoServiceImpl(RechargeInfoMapper rechargeInfoMapper, UserAccountService userAccountService) {
		this.rechargeInfoMapper = rechargeInfoMapper;
		this.userAccountService = userAccountService;
	}

	/**
	 * @description: 账户充值功能 保存充值订单 订单状态为未支付
	 * @author Petrichor
	 * @date 2024/1/11 10:11
	 * @param rechargeInfoVo 充值对象 [充值金额  	支付方式：1101-微信 1102-支付宝]
	 * @return 订单号
	 **/
	@Override
	public String submitRecharge(RechargeInfoVo rechargeInfoVo) {
		// 获取登录用户id
		Long userId = AuthContextHolder.getUserId();
		// 创建实体类
		RechargeInfo rechargeInfo = new RechargeInfo();
		// 用户ID
		rechargeInfo.setUserId(userId);
		// 充值订单编号
		rechargeInfo.setOrderNo(UUID.randomUUID().toString().replaceAll("-",""));
		// 充值状态：0901-未支付 0902-已支付 0903-已取消"
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
		// 充值金额
		rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
		// 支付方式：1101-微信 1102-支付宝
		rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());

		return rechargeInfo.getOrderNo();
	}
	/**
	 * @description: 根据订单号获取充值信息
	 * @author Petrichor
	 * @date 2024/1/11 10:34
	 * @param orderNo 订单号
	 * @return 充值订单信息
	 **/
	@Override
	public RechargeInfo getRechargeInfoByOrderNo(String orderNo) {
		LambdaQueryWrapper<RechargeInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		LambdaQueryWrapper<RechargeInfo> eq = lambdaQueryWrapper.eq(RechargeInfo::getOrderNo, orderNo);
		RechargeInfo rechargeInfo = this.getOne(eq);
		return rechargeInfo;
	}
	/**
	 * @description: 更新充值的消息
	 * @author Petrichor
	 * @date 2024/1/10 20:21
	 * @param orderNo 订单号
	 * @return
	 **/
	@Override
	public void rechargePaySuccess(String orderNo) {
		// 获取到充值信息对象
		RechargeInfo rechargeInfo = this.getRechargeInfoByOrderNo(orderNo);
		if (SystemConstant.ORDER_STATUS_PAID.equals(rechargeInfo.getRechargeStatus())) {
			return ;
		}
		rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
		this.updateById(rechargeInfo);

		// 更新余额
		userAccountService.add(rechargeInfo.getUserId(),
				rechargeInfo.getRechargeAmount(),
				rechargeInfo.getOrderNo(),
				SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT ,
				"充值");
	}
}
